// 功能开启状态监听

// const { doc } = require('prettier');

/* 获取图元 PCB_SelectControl.getAllSelectedPrimitives() 查询所有已选中图元的图元对象
// 构造函数如下
function getClickAllValue(getAllData) {
	this.libraryUuid = getAllData.footprint.libraryUuid;
	console.log(this.libraryUuid);
	this.uniqueId = getAllData.uniqueId;
	console.log(this.uniqueId);
	this.pinNumber = getAllData.pads.length;
	console.log(this.pinNumber);
	this.designator = getAllData.designator;
	console.log(this.designator);
	this.x = getAllData.x;
	console.log(this.x);
	this.y = getAllData.y;
	console.log(this.y);
} */
let partNumber = '';
let uniqueID = '';
let X_coordinate = '';
let Y_coordinate = '';
let img;

window.onload = function () {
	partNumber = document.getElementById('part-number');
	uniqueID = document.getElementById('unique-id');
	X_coordinate = document.getElementById('x-coordinate');
	Y_coordinate = document.getElementById('y-coordinate');
};

function stringToImage(str, textHeight) {
	// 删除此前页面上有的img元素
	document.querySelector('.image_show').innerHTML = '';
	// 创建一个canvas元素
	const canvas = document.createElement('canvas');
	const ctx = canvas.getContext('2d');
	// 设置canvas的宽度和高度
	canvas.width = 300;
	canvas.height = 150;
	// 设置字体样式
	ctx.font = `${textHeight}px Arial`;
	const textWidth = ctx.measureText(str).width;
	// 绘制文本
	ctx.fillText(str, (canvas.width - textWidth) / 2, textHeight);
	// 将canvas内容展示在页面上
	const imgValue = canvas.toDataURL('image/png');
	let img = new Image();
	img.src = imgValue;
	console.log(img);
	document.querySelector('.image_show').appendChild(img);
	return img;
}
// 将图片格式从base64转换为blob
function base64ToBlob(base64, contentType) {
	const base64Message = base64.split(',')[1]; // 去除
	const byteCharacters = atob(base64Message);
	let byteArrays = [];
	for (let i = 0; i < byteCharacters.length; i++) {
		byteArrays.push(byteCharacters.charCodeAt(i));
	}
	const blob = new Blob([new Uint8Array(byteArrays)], { type: contentType });
	console.log(blob);
	return blob;
}

// 将获取到的参数data显示在页面上
eda.pcb_Event.addMouseEventListener(
	'getClickAllData',
	'selected',
	async () => {
		const getAll_PrimitivesId = await eda.pcb_SelectControl.getAllSelectedPrimitives_PrimitiveId(); // 获取图元ID
		console.log('点击事件触发一次');
		const getAll_primitives = await eda.pcb_PrimitivePad.get(getAll_PrimitivesId); // 通过ID获取图元参数
		if (getAll_primitives) {
			// 判断是否获取到图元参数
			for (const i of getAll_primitives) {
				// const aaaa = new getBondingPad(i);
				// console.log(aaaa);
				// 由于可能有多个原件参数以数组的类型输入，故采取遍历的方式
				console.log(i.net);
				console.log(i.primitiveId);
				console.log(i.x);
				console.log(i.y);
				partNumber.innerHTML = i.net;
				uniqueID.innerHTML = i.primitiveId;
				X_coordinate.innerHTML = i.x;
				Y_coordinate.innerHTML = i.y;
				// 将获取到的参数data显示在页面上
				img = stringToImage(`${partNumber.innerHTML}`, 50);
			}
		}
	},
	false,
);
btn = document.querySelector('.generate-button');
btn.addEventListener('click', async () => {
	console.log('生成事件触发一次');
	// 获取图片
	const imageBlob = base64ToBlob(img.src, 'image/png'); // 将图片格式从base64转换为blob
	const edaImage = await eda.pcb_MathPolygon.convertImageToComplexPolygon(imageBlob, 30, 30, 0.5, 0.9, 1, 2, false, false); // 将blob转为复杂多边形对象
	eda.pcb_PrimitiveImage.create(X_coordinate.innerHTML, Y_coordinate.innerHTML, edaImage, EPCB_LayerId.TOP_SILKSCREEN, 20, 30, 0, false, false);
});
// eda.pcb_PrimitiveImage.create(0, 0, edaImage, EPCB_LayerId.TOP_SILKSCREEN, 20, 30, 0, false, false);
// const getClickAllData = await eda.pcb_SelectControl.getAllSelectedPrimitives();
// console.log(getClickAllData);
// pcb_Layer 图层操作
